package com.example.java_gobang.game;

import com.example.java_gobang.model.User;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

//管理游戏对决的创建和结束
@Component
public class RoomManager {
    //房间id 和游戏房间
    private  ConcurrentHashMap<String, Room> rooms = new ConcurrentHashMap<>();
    //用户id 和房间 id
    private  ConcurrentHashMap<Integer,String> userIdToRoomId = new ConcurrentHashMap<>();

    public void addTwoUserBattle(Room room, User use1, User user2){
        rooms.put(room.getRoomId(),room);
        userIdToRoomId.put(use1.getUserId(),room.getRoomId());
        userIdToRoomId.put(user2.getUserId(),room.getRoomId());
    }


    public void removeTwoUserBattle(Room room, User use1, User user2){
        rooms.remove(room.getRoomId(),room);
        userIdToRoomId.remove(use1.getUserId(),room.getRoomId());
        userIdToRoomId.remove(user2.getUserId(),room.getRoomId());
    }

    public Room getRoomByRoomId(String RoomId){
        return rooms.get(RoomId);
    }

    public Room getRoomByUserId(int userid){
        if ((userIdToRoomId.get(userid) == null)){
            return null;
        }
        return rooms.get(userIdToRoomId.get(userid));
    }


}
